<?php



namespace plugins\member_login\controller;



use think\Db;



use plugins\member_login\validate\MemberValidate;



use plugins\member\model\MemberModel;



use cmf\controller\ApiThirdAccessBaseController;

use plugins\member\interfaceLib\MemberClassify;

use plugins\finance\model\FinanceModel;



/**



 * 手机号码控制器



 */



class ApiPhoneController extends ApiThirdAccessBaseController

{

	public function regiest($param)
	{

		//参数验证
		// $result = $this->validate( $param['data'] , 'Api.phoneRegiest' , [ 'phone.require'=>'手机号不能为空' , 'code.require' => '验证码不能为空' ]);
		$checkParam['phone'] = ['name'=>'手机号码','type'=>'require|regex:/^1[34578]\d{9}$/','message'=>['require'=>'手机号码不能为空','regex'=>'请输入正确的手机号码']];
		// $checkParam['password'] = ['name'=>'登录密码'];
		$checkParam['code'] = ['name'=>'验证码'];
		// $checkParam
		checkParam($checkParam,$param['data']);


		//查重
		$check = MemberModel::get(['phone' => $param['data']['phone']]);
		if($check){
			return api_json_echo( false , '该手机号已注册' );
		}

		//手机验证码检测
//		$check_code = cmf_check_verification_code($param['data']['phone'], $param['data']['code'],true);
//		if($check_code != ''){
//			return api_json_echo( false , $check_code);
//		}

		//注册参数

		$add['phone'] = $param['data']['phone'];

		// $add['nick_name'] = $param['data']['nick_name'];

		//随机的登录名

		$add['login_name'] = "user_".substr(uniqid(),0,6).mt_rand(1000,9999);

		$add['join_date'] = time();

		$add['join_ip'] = $this->request->ip();

		$add['password'] = empty($param['data']['password']) ? '' : cmf_password($param['data']['password']);

		//默认头像
		$add['head_portrait'] = '/plugins/member/view/public/user.png';

		$m = new MemberModel( $add );

		$result =  $m->allowField( true )->save();

		if( !$result ){
			return api_json_echo( false , '注册失败' );
		}

		//uid的补充
	    $res=str_pad($m->id,4,"0",STR_PAD_LEFT);   
		$uid = 'zy_'.$res.mt_rand(1000,9999);
		$model = MemberModel::get($m->id);
		$model->uid = $uid;
		$model->nick_name = $uid;

		//加密的uid
		$aes = new MemberClassify;
		$model->uid_aes = $aes->aesSetUid($uid);
		$model->save();


        if(!empty($param['data']['openid'])){
            Db::name('member')->where('uid',$uid)->update([ 'openid' => $param['data']['openid']]);
        }

		//返回反馈信息
		$data['token'] = $this->setIdentity($uid);
		$data['refresh_token'] = $this->setRefreshToken($uid);	
		$data['aes'] = $model->uid_aes;

		//创建财务账户
		( new FinanceModel )->createFinanceAccount('member',$uid);

		return api_json_echo( true , '会员注册成功',$data );

	}



	//手机验证码登陆
	public function login($param)
	{
		//验证参数
		// $result = $this->validate( $param['data'] , 'Api.phoneLogin' );

		$checkParam['phone'] = ['name'=>'手机号码','type'=>'require|regex:/^1[34578]\d{9}$/','message'=>['regex'=>'请输入正确的手机号码']];
		$checkParam['code'] = ['name'=>'验证码'];
		checkParam($checkParam,$param['data']);

		//检测
		//没有直接注册    phone code
		$check_member = ( new MemberModel )->get(['phone'=>$param['data']['phone']]);
		//该手机没有注册
		if($check_member == null){
			$this->regiest($param);
		}

		//手机验证码检测
		$check_code = cmf_check_verification_code($param['data']['phone'], $param['data']['code'],true);

		if($check_code != ''){
			return api_json_echo( false , $check_code);
		}		

		$check = MemberModel::get(['phone'=>$param['data']['phone']]);

		if($check->status == 1){
			//修改登陆时间与ip
			$Member = ( new MemberModel )->get([ 'phone' => $param['data']['phone'] , 'status' => 1 ]);
			$result = $Member->allowField(true)->save([ 'last_date' => time() , 'last_ip' => $this->request->ip() ]);
			if($result){


                if(!empty($param['data']['openid'])){
                    $Member->allowField(true)->save([ 'openid' => $param['data']['openid']]);
                }


				//生成token
				$data['token'] = $this->setIdentity($check->uid);
				$data['refresh_token'] = $this->setRefreshToken($check->uid);
				$data['aes'] = $check->uid_aes;

				return api_json_echo( true , '会员登录成功' , $data );
			}else{
				return api_json_echo( false , '会员登录失败，请检查号码' );
			}
		}else if($check->status == 2){
			return api_json_echo( false , '该用户被禁用，请重试' );
		}else if($check->status == 3){

		}
		return api_json_echo( false , '会员登录失败，请检查号码' );
	}



}